#include "filewin.h"
#include "ui_filewin.h"
#include <QFileDialog>
#include <QDebug>

filewin::filewin(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::filewin)
{
    ui->setupUi(this);
}

filewin::~filewin()
{
    delete ui;
}

void filewin::on_new_action_triggered()
{
    //获取新建文件路径
    QString filename = QFileDialog::getSaveFileName(this);
    qDebug()<<filename;
    //1.创建文件路径
    QFile file(filename);
    //2.打开路径
    if(!file.open(QIODevice::ReadWrite)){
        qDebug()<<"open file!!";
        return;
    }
    //3.读取编辑框数据
    QString data = ui->textEdit->toPlainText();
    file.write(data.toUtf8());

    //4.关闭文件
    file.close();

}

void filewin::on_quit_action_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this);
    //文件操作
    //1.创建文件对象
    QFile file(filename);
    //2.打开文件
    if(!file.open(QIODevice::ReadWrite)){
        qDebug()<<"open file!!";
        return;
    }

    //3.读取文件数据
    QString str =file.readAll();

    ui->textEdit->setText(str);
    qDebug()<<str;

    //4.关闭文件
    file.close();
}

void filewin::on_open_action_triggered()
{
    this->close();
}
